package com.android.myapplication.third.greendao;

import android.app.Activity;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.InputEvent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.myapplication.App;
import com.android.myapplication.R;
import com.android.myapplication.view.adapter.StudentBean;
import com.greendao.gen.DaoSession;
import com.greendao.gen.TeacherBeanDao;

import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AlertDialog;

public class GreenDaoActivity extends Activity {
    ListView mList;
    EditText mEditText;
    ArrayAdapter<TeacherBean> mAdapter;
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_greendao);
         mList  = findViewById(R.id.list);
         mEditText = findViewById(R.id.ed_name);

        DaoSession daoSession = ((App)getApplication()).getDaoSession();

        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    add();
                    return true;
                }
                return false;
            }
        });
        findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add();
            }
        });
        mList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                final TeacherBean teacherBean = mAdapter.getItem(position);
                new AlertDialog.Builder(GreenDaoActivity.this).setTitle("删除").setMessage("确认删除"+teacherBean.getName()+"吗？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((App)getApplication()).getDaoSession().getTeacherBeanDao().delete(teacherBean);

                        upList();
                        dialog.dismiss();
                    }
                }).show();
                return false;
            }
        });
        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final TeacherBean teacherBean = mAdapter.getItem(position);
                Intent intent = new Intent(GreenDaoActivity.this, StudentDaoActivity.class);
                intent.putExtra("id", teacherBean.getId());
                startActivityForResult(intent, 1);

            }
        });
        upList();
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1){
            upList();
        }

    }


    private void add(){
        TeacherBean teacherBean = new TeacherBean();
        teacherBean.setName(mEditText.getText().toString());
        ((App)getApplication()).getDaoSession().getTeacherBeanDao().insert(teacherBean);
        mEditText.setText("");
        upList();
    }
    private void upList(){
        TeacherBeanDao teacherBeanDao = ((App)getApplication()).getDaoSession().getTeacherBeanDao();
        QueryBuilder builder = teacherBeanDao.queryBuilder().where(TeacherBeanDao.Properties.Name.eq("2"));
        Query<TeacherBean> teacherBeanQuery = builder.build();
        List<TeacherBean> list = teacherBeanQuery.list();

        mAdapter = new ArrayAdapter<TeacherBean>(this,android.R.layout.simple_list_item_1, list);
        mList.setAdapter(mAdapter);

    }
}
